package com.example.zhinongbao.activity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zhinongbao.R;

public class NewsDetailActivity extends AppCompatActivity {

    private WebView webView;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        tvTitle = findViewById(R.id.tv_news_title_detail);
        webView = findViewById(R.id.webview_news);

        String title = getIntent().getStringExtra("title");
        String htmlContent = getIntent().getStringExtra("htmlContent");

        tvTitle.setText(title);
        initWebViewSettings();

        // 加载HTML内容（使用loadDataWithBaseURL处理图片路径）
        webView.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
    }

    private void initWebViewSettings() {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setUseWideViewPort(true);
        settings.setLoadsImagesAutomatically(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
}